package cn.yangtianxia.org.controller;

import cn.yangtianxia.basic.util.AjaxResult;
import cn.yangtianxia.org.domain.SystemDictionaryDetail;

import cn.yangtianxia.org.service.ISystemDictionaryDetailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class SystemDictionaryDetailController {

    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail){
        try {
            if(systemDictionaryDetail.getId() != null){
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }else{
                systemDictionaryDetailService.save(systemDictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            systemDictionaryDetailService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me() .setSuccess(false) .setMessage("系统繁忙，稍后重试！！！");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public SystemDictionaryDetail queryById(@PathVariable("id") Long id){
        return  systemDictionaryDetailService.loadById(id);
    }


    @GetMapping
    public List<SystemDictionaryDetail> queryAll(){
        return  systemDictionaryDetailService.loadAll();
    }

    @PostMapping("/{id}")
    public List<SystemDictionaryDetail> queryPage(@PathVariable("id") Long id){
        return  systemDictionaryDetailService.queryId(id);
    }
}
